package com.famdoc.Utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

import static com.famdoc.Utils.MqConstants.DELAYED_EXCHANGE_NAME;
import static com.famdoc.Utils.MqConstants.DELAYED_ROUTING_KEY;

/**
 * 发送队列工具类
 */
@Component
@Slf4j
public class SendMQUtils {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送随访预约队列
     * @param id
     * @param visitTime
     */
    public void sendVisitMQ(Integer id, LocalDateTime visitTime) {
        LocalDateTime now = LocalDateTime.now();
        long millis = ChronoUnit.MILLIS.between(now,visitTime);
        //发送消息
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY,
                String.valueOf(id),
                correlationData ->{
                    correlationData.getMessageProperties().setDelay((int) millis);
                    return correlationData;
                });
        log.info(" 当 前 时 间 ： {}, 发送一条延迟 {} 毫秒的信息给队列 delayed.queue:{}", new
                Date(),millis, id);
    }
}
